package io.renren.service.impl;

import com.alibaba.fastjson.JSONObject;
import io.renren.dao.PersonDispatchDao;
import io.renren.entity.PersonDispatchEntity;
import io.renren.utils.DateCompare;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

import io.renren.dao.JobConfirmDao;
import io.renren.entity.JobConfirmEntity;
import io.renren.service.JobConfirmService;



@Service("jobConfirmService")
public class JobConfirmServiceImpl implements JobConfirmService {
	@Autowired
	private JobConfirmDao jobConfirmDao;

	@Autowired
	private PersonDispatchDao personDispatchDao;
	
	@Override
	public JobConfirmEntity queryObject(Integer id){
		return jobConfirmDao.queryObject(id);
	}
	
	@Override
	public List<JobConfirmEntity> queryList(Map<String, Object> map){
		return jobConfirmDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return jobConfirmDao.queryTotal(map);
	}
	
	@Override
	public void save(JobConfirmEntity jobConfirm){
		jobConfirmDao.save(jobConfirm);
	}

	@Override
	public void update(JobConfirmEntity jobConfirm){
		jobConfirmDao.update(jobConfirm);
	}
	
	@Override
	public void delete(Integer id){
		jobConfirmDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		jobConfirmDao.deleteBatch(ids);
	}

	@Override
	public void updateConfirmStatus(JSONObject jsonObject) {
		Calendar ca = Calendar.getInstance();

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
		String date = format.format(ca.getTime());

		ca.setTime(new Date());
		ca.add(Calendar.MONTH, -1);
		String idss = jsonObject.getString("ids");
		System.out.println(idss.substring(1,idss.length()-1));
		String[] ids = idss.substring(1,idss.length()-1).split(",");
		String confirm = jsonObject.getString("confirm");
		Map<String, Object> map = new HashMap<>();
		if("confirm".equals(confirm)){//上岗状态(0,待上岗,1,审核通过,2,审核未通过,3,反审)
			map.put("status","1");
			for (String id : ids) {
				id = id.substring(1,id.length()-1);
				JobConfirmEntity jobConfirmEntity = jobConfirmDao.queryObject(id);
				int empid = jobConfirmEntity.getEmpid();
				PersonDispatchEntity person = personDispatchDao.queryObject(empid);
				String batch = "";
				//人员派遣表中有该员工记录
				if(person!=null){
					Date injuryInsuredDate = person.getInjuryInsuredDate();
					int res = DateCompare.compare_date(injuryInsuredDate, ca.getTime());
					//工伤保险申报日期要早于当前日期往前一个月的日期，才能审核
					if(res==-1){
						map.put("batch",date);
						map.put("id",id);
						jobConfirmDao.confirmWorkStatus(map);
					}
				}
			}
		}else if("confirmByPerson".equals(confirm)){
			map.put("status","1");
			for (String id : ids) {
				id = id.substring(1, id.length() - 1);
				map.put("batch",date);
				map.put("id",id);
				jobConfirmDao.confirmWorkStatus(map);
			}
		}else if("failedConfirm".equals(confirm)){
			map.put("status","2");
			for (String id : ids) {
				id = id.substring(1, id.length() - 1);
				map.put("id",id);
				jobConfirmDao.confirmWorkStatus(map);
			}
		}else if("recovertConfirm".equals(confirm)){
			map.put("status","3");
			for (String id : ids) {
				id = id.substring(1, id.length() - 1);
				map.put("id",id);
				jobConfirmDao.confirmWorkStatus(map);
			}
		}
	}

	@Override
	public List<JobConfirmEntity> getInfoByParams(Map<String, Object> map) {
		return jobConfirmDao.getInfoByParams(map) ;
	}

}
